﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;

namespace PR.DataAccess.RamanSyrtsou
{
    public class DataAccessManager : IDisposable
    {
        private DataContext context;

        public DataAccessManager()
        {
            context = new DataContext("server=localhost;database=PRWORK;Integrated Security=true");
        }

        public List<PR_Object> GetAllObjects()
        {
            return context.GetTable<PR_Object>().ToList();
        }

        public void DeletePRObject(int id)
        {
            PR_Object obj = context.GetTable<PR_Object>().Where(o => o.PR_ObjectID == id).FirstOrDefault();

            if (obj != null)
            {
                context.GetTable<PR_Object>().DeleteOnSubmit(obj);
                context.SubmitChanges();
            }

        }

        public int InsertPRObject(PR_Object obj)
        {
            context.GetTable<PR_Object>().InsertOnSubmit(obj);
            context.SubmitChanges();

            return obj.PR_ObjectID;
        }

        public List<PR_Type> GetAllTypes()
        {
            return context.GetTable<PR_Type>().ToList();
        }

        public List<PR_Category> GetAllCategories()
        {
            return context.GetTable<PR_Category>().ToList();
        }

        public PR_Object GetObjectById(int id)
        {
            return context.GetTable<PR_Object>().Where(obj => obj.PR_ObjectID == id).FirstOrDefault();
        }

        public void UpdateObject(PR_Object obj)
        {
            var loadedObj = GetObjectById(obj.PR_ObjectID);

            loadedObj.Name = obj.Name;
            loadedObj.PR_CategoryFK = obj.PR_CategoryFK;
            loadedObj.PR_TypeFK = obj.PR_TypeFK;

            context.SubmitChanges();
        }

        public PR_Description GetDescriptionById(int id)
        {
            return context.GetTable<PR_Description>().Where(desc => desc.PR_DescriptionID == id).FirstOrDefault();
        }

        public int InsertDescription(PR_Description description)
        {
            context.GetTable<PR_Description>().InsertOnSubmit(description);
            context.SubmitChanges();

            return description.PR_DescriptionID;
        }

        public void UpdateDescription(PR_Description description)
        {
            var loadedDescription = GetDescriptionById(description.PR_DescriptionID);

            loadedDescription.Description = description.Description;
            loadedDescription.PR_DescriptionID = description.PR_DescriptionID;
            loadedDescription.PR_ObjectFK = description.PR_ObjectFK;

            context.SubmitChanges();
        }

        public void DeleteDescription(int id)
        {
            PR_Description description = context.GetTable<PR_Description>().Where(d => d.PR_DescriptionID == id).FirstOrDefault();

            if (description != null)
            {
                context.GetTable<PR_Description>().DeleteOnSubmit(description);
                context.SubmitChanges();
            }
        }

        public List<PR_Description> GetDescriptionsByObjectId(int id)
        {
            return context.GetTable<PR_Description>().Where(d => d.PR_ObjectFK == id).ToList();
        }

        public void Dispose()
        {
            context.Dispose();
        }
    }
}
